package com.liang.lollipop.lreader.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.liang.lollipop.lreader.bean.ModelBean;

import java.util.ArrayList;

/**
 * Created by Lollipop on 2017/09/06.
 * 模块数据操作的对象
 */
public class ModelDAO {

    private Context context;
    private DBUtil dbUtil;

    public ModelDAO(Context context) {
        this.context = context;
        dbUtil = new DBUtil(context);
    }

    public long insert(ModelBean bean){
        SQLiteDatabase database = dbUtil.getWritableDatabase();
        database.beginTransaction();
        try{
            ContentValues contentValues = new ContentValues();
            contentValues.put(DBUtil.MODEL_NAME,bean.name);
            contentValues.put(DBUtil.MODEL_VALUE,bean.value);
            contentValues.put(DBUtil.MODEL_PROJECT,bean.project);
            contentValues.put(DBUtil.MODEL_SORT,bean.sort);
            long l = database.insert(DBUtil.MODEL_TABLE,null,contentValues);
            database.setTransactionSuccessful();
            return l;
        }finally {
            database.endTransaction();
        }
    }

    public int update(ModelBean bean){
        SQLiteDatabase database = dbUtil.getWritableDatabase();
        database.beginTransaction();
        try{
            ContentValues contentValues = new ContentValues();
            contentValues.put(DBUtil.MODEL_NAME,bean.name);
            contentValues.put(DBUtil.MODEL_VALUE,bean.value);
            contentValues.put(DBUtil.MODEL_PROJECT,bean.project);
            contentValues.put(DBUtil.MODEL_SORT,bean.sort);
            int i = database.update(DBUtil.MODEL_TABLE,contentValues,DBUtil.MODEL_ID+" = ?",new String[]{String.valueOf(bean.id)});
            database.setTransactionSuccessful();
            return i;
        }finally {
            database.endTransaction();
        }
    }

    public int delete(int id){
        SQLiteDatabase database = dbUtil.getWritableDatabase();
        database.beginTransaction();
        try {
            int i = database.delete(DBUtil.MODEL_TABLE,DBUtil.MODEL_ID+" = ?",new String[]{String.valueOf(id)});
            database.setTransactionSuccessful();
            return i;
        }finally {
            database.endTransaction();
        }
    }

    public ArrayList<ModelBean> selectAll(){
        SQLiteDatabase database = dbUtil.getReadableDatabase();
        ArrayList<ModelBean> beanList = new ArrayList<>();
        Cursor cursor = database.query(DBUtil.MODEL_TABLE,
                new String[]{DBUtil.MODEL_ID,DBUtil.MODEL_NAME,DBUtil.MODEL_VALUE,DBUtil.MODEL_PROJECT,DBUtil.MODEL_SORT},
                null,null,null,null,DBUtil.MODEL_SORT);
        while(cursor.moveToNext()){
            ModelBean bean = new ModelBean();
            bean.id = cursor.getInt(cursor.getColumnIndex(DBUtil.MODEL_ID));
            bean.name = cursor.getString(cursor.getColumnIndex(DBUtil.MODEL_NAME));
            bean.value = cursor.getInt(cursor.getColumnIndex(DBUtil.MODEL_VALUE));
            bean.project = cursor.getString(cursor.getColumnIndex(DBUtil.MODEL_PROJECT));
            bean.sort = cursor.getInt(cursor.getColumnIndex(DBUtil.MODEL_SORT));
            beanList.add(bean);
        }
        cursor.close();
        return beanList;
    }

    public ModelBean selectFirst(){
        SQLiteDatabase database = dbUtil.getReadableDatabase();
        Cursor cursor = database.query(DBUtil.MODEL_TABLE,
                new String[]{DBUtil.MODEL_ID,DBUtil.MODEL_NAME,DBUtil.MODEL_VALUE,DBUtil.MODEL_PROJECT,DBUtil.MODEL_SORT},
                null,null,null,null,DBUtil.MODEL_SORT);
        if(cursor.moveToNext()){
            ModelBean bean = new ModelBean();
            bean.id = cursor.getInt(cursor.getColumnIndex(DBUtil.MODEL_ID));
            bean.name = cursor.getString(cursor.getColumnIndex(DBUtil.MODEL_NAME));
            bean.value = cursor.getInt(cursor.getColumnIndex(DBUtil.MODEL_VALUE));
            bean.project = cursor.getString(cursor.getColumnIndex(DBUtil.MODEL_PROJECT));
            bean.sort = cursor.getInt(cursor.getColumnIndex(DBUtil.MODEL_SORT));
            return bean;
        }
        cursor.close();
        return null;
    }

    //交换位置，包含事务处理，但处理失败，则回退
    public boolean updates(ModelBean... been){
        SQLiteDatabase database = dbUtil.getWritableDatabase();
        database.beginTransaction();
        try {
            for(ModelBean bean:been){
                ContentValues contentValues = new ContentValues();
                contentValues.put(DBUtil.MODEL_SORT,bean.sort);
                contentValues.put(DBUtil.MODEL_NAME,bean.name);
                contentValues.put(DBUtil.MODEL_VALUE,bean.value);
                contentValues.put(DBUtil.MODEL_PROJECT,bean.project);
                database.update(DBUtil.MODEL_TABLE,contentValues,DBUtil.MODEL_ID+" = ?",new String[]{String.valueOf(bean.id)});
            }
            database.setTransactionSuccessful();
            return true;
        }finally {
            database.endTransaction();
        }
    }

}


